package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.constant.MqConst;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class PaymentServiceImpl implements PaymentService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitService rabbitService;
    /**
     * 保存支付信息
     * @param orderInfo
     * @param name
     */
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        //先查询，看是否有记录
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id", orderInfo.getId());
        wrapper.eq("payment_type",paymentType);
        Integer count = paymentInfoMapper.selectCount(wrapper);
        //如果有证明已经保存过了，不做处理
        if (count>0){
            return;
        }
        // 保存交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfoMapper.insert(paymentInfo);
    }

    /**
     * 关闭交易记录
     * @param orderId
     */
    @Override
    public void closePayment(Long orderId) {
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        Integer count = paymentInfoMapper.selectCount(wrapper);
        if (count==0){
            return;
        }
        QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.eq("payment_status","UNPAID");
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());
        paymentInfoMapper.update(paymentInfo,queryWrapper);
    }

    @Override
    public void paySuccess(String outTradeNo, String paymentType, Map<String, String> paramMap) {
        PaymentInfo paymentInfo1 = getPaymentInfo(outTradeNo, paymentType);
        if (paymentInfo1==null){
            return;
        }
        try {
            PaymentInfo paymentInfo=new PaymentInfo();
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            paymentInfo.setCallbackContent(paramMap.toString());
            paymentInfo.setTradeNo(paramMap.get("trade_no"));
            updatePaymentInfo(outTradeNo,paymentType,paymentInfo);
        } catch (Exception e) {
            redisTemplate.delete(paramMap.get("notify_id"));
            e.printStackTrace();
        }
        //修改订单状态
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfo1.getOrderId());
    }

    /**
     * 修改订单
     * @param outTradeNo
     * @param name
     * @param paramsMap
     */
    @Override
    public void updatePaymentInfo(String outTradeNo, String paymentType, PaymentInfo paymentInfo) {
        QueryWrapper<PaymentInfo> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("out_trade_no",outTradeNo);
        QueryWrapper.eq("payment_type",paymentType);
        paymentInfoMapper.update(paymentInfo,QueryWrapper);
    }

    /**
     * 查询支付记录
     * @param outTradeNo
     * @param name
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
        QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper();
        queryWrapper.eq("out_trade_no",outTradeNo);
        queryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(queryWrapper);
        return paymentInfo;
    }
}
